<?php
namespace app\a\controller;
use qiniu\Qn;
use think\facade\Db;
class Other extends Base{

    //修改登录密码
    public function upPass_a(){
        $param = input('');
        $pass = md5('anan'.md5(trim($param['pass_a'])));
        $where = [
            'id'=>$param['id'],
            'pass'=> md5('anan'.md5(trim($param['oldpass'])))
        ];
        $adminData = Db::table('an_admin')->where($where)->select()->toArray();
        if(count($adminData) > 0){
            $bool = Db::table('an_admin')->where('id', $param['id'])->data(['pass'=>$pass])->update();
            session('adminData', null);
            $this->logData('修改系统登录密码',$param['token']);
            $this->info['code']=200;
            $this->info['msg']='修改成功';
        }else{
            $this->info['code']=201;
            $this->info['data']=$adminData;
            $this->info['msg']='修改失败,原密码输入有误';
        }
        return json($this->info);
    }

    //获取七牛云token
    public function getQiNiuToken(){
        $param = input('');
        $qn = new Qn();
        $this->info['data'] = $qn->getToken($param['name']); 
        $this->info['msg'] = '获取成功';
        return json($this->info);
    }

    //获取七牛云数据列表
    public function getQiniuFile(){
        $param = input('');

        Db::table('an_img')->where('prefix', $param['prefix'])->delete();

        $qn = new Qn();

        //获取七牛云空间文件
        $this->info['data'] = $qn->getData($param['name'],$param['limit'],$param['marker'],$param['prefix'])[0]; 

        //批量 同步七牛云空间图片到数据库 an_img表
        if($param['is_sync'] == '1'){
            foreach($this->info['data']['items'] as $k=>$v){
                Db::table('an_img')->insert([
                    'create_time'=>time(),
                    'prefix'=>$param['prefix'],
                    'key'=>$v['key'],
                ]); 
            }
        }
        return json($this->info);
    }

    //删除七牛云文件
    public function delQiniuFile(){
        $param = input('');
        $qn = new Qn();
        $this->info['data'] = $qn->delData($param['key'],$param['space']);
        $this->info['code'] = 200;
        return json($this->info);
    }


    public function async_server_img(){
        $path = './uploads';

        //删除
        Db::table('an_img')->where('prefix', 'local')->delete();

        // 打开path目录 获取目录资源
        if ($dh = opendir($path)) {
    
            //循环获取目录资源下除了 . 且 .. 的每个目录名称 
            while (($name = readdir($dh)) !== false) {
                if ($name != "." && $name != "..") {
                    
                    //打开目录 获取目录下所有文件
                   $list = array_diff(scandir($path.'/'.$name), array('.', '..'));
          
                   //循环所有文件，拼接完整路径 写入数据库
                    foreach($list as $v){
                        // dump($name.'/'.$v);
                        Db::table('an_img')->insert([
                            'create_time'=>time(),
                            'prefix'=>'local',
                            'key'=>$name.'/'.$v,
                        ]); 
                    }
              
                }
            }
        }
        closedir($dh);//关闭文件操作
    }
}
